package com.doctor.ui.homedoctor;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

import com.doctor.adapter.ShowImageAdapter;
import com.doctor.bean.DiseaseRecordBean;
import com.doctor.comm.ConstConfig;
import com.doctor.comm.DialogHelper;
import com.doctor.database.DbOperator;
import com.doctor.ui.BaseActivity;
import com.doctor.ui.R;
import com.doctor.view.NoScrollGridView;

public class AnswerPoneDiseaseActivity extends BaseActivity{

	/**
	 * 接听电话查看病历
	 */
	
	private TextView startDay,endDay,disease,hospital,content;
	private DiseaseRecordBean bean;
	//显示图片之用
	private NoScrollGridView gridView;
	private ShowImageAdapter adapter;
	private List<String> beanList;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.answer_phone_disease_record);
		
		bean=(DiseaseRecordBean) getIntent().getSerializableExtra(ConstConfig.BEAN);
		initiaView();
	}
	
	@Override
	protected void initiaView() {
		// TODO Auto-generated method stub
		startDay=(TextView) findViewById(R.id.startDay);
		startDay.setText(bean.getStartDay());
		
		endDay=(TextView) findViewById(R.id.endday);
		endDay.setText(bean.getEndDay());
		
		disease=(TextView) findViewById(R.id.disease);
		disease.setText(bean.getDisease());
		
		hospital=(TextView) findViewById(R.id.hospital);
		hospital.setText(bean.getHospital());
		
		content=(TextView) findViewById(R.id.content);
		content.setText(bean.getContent());
		
		gridView=(NoScrollGridView) findViewById(R.id.gridView);
		beanList=new ArrayList<String>();
		adapter=new ShowImageAdapter(AnswerPoneDiseaseActivity.this, beanList);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				String pic=(String) arg1.getTag(R.id.bean_tag);
				DialogHelper.showImageDialog(AnswerPoneDiseaseActivity.this, Uri.fromFile(new File(pic)));
			}
		});
		gridView.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				//无实际作用，主要防止长按后响应onItemClick事件
				return true;
			}
		});
		getImageFromDb();
	}
	
	
	/**
	 * 从数据库查询图片uri
	 */
	private void getImageFromDb(){
		new Thread() {
			public void run() {
				try {
					sleep(DbOperator.delayTime);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				String images=DbOperator.getInstance(AnswerPoneDiseaseActivity.this).selectImages(ConstConfig.DISEASE_RECORD_TABLE, bean.getId());
				if (images!=null && !"".equals(images)) {
					String[] imageArray= images.split(ConstConfig.IMAGE_SPLIT);
					for (String image : imageArray) {
						beanList.add(image);
					}
					runOnUiThread(new Runnable() {
						public void run() {
							adapter.notifyDataSetChanged();
						}
					});
					
				}
			}
		}.start();
	}
	
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (adapter != null) {
			adapter.clear();
		}
	}
	
}
